package com.yanggu.bigdata.service.impl;

import cn.hutool.json.JSONObject;
import com.yanggu.bigdata.entity.po.ProductStats;
import com.yanggu.bigdata.mapper.ProductStatsMapper;
import com.yanggu.bigdata.service.ProductStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductStatsServiceImpl implements ProductStatsService {

    @Autowired
    private ProductStatsMapper productStatsMapper;

    @Override
    public BigDecimal getGMV(int date) {
        return productStatsMapper.getGMV(date);
    }

    @Override
    public Map<String, List<JSONObject>> getProductStatsGroupBySpu(int date, int limit) {
        Map<String, List<JSONObject>> data = new HashMap<>(2);
        List<JSONObject> columns = Arrays.asList(new JSONObject().set("name", "商品名称").set("id", "spu_name"),
                new JSONObject().set("name", "交易额").set("id", "order_amount"));

        data.put("columns", columns);

        List<ProductStats> dataList = productStatsMapper.getProductStatsGroupBySpu(date, limit);
        List<JSONObject> rows = dataList.stream()
                .map(temp -> new JSONObject()
                        .set("spu_name", temp.getSpu_name())
                        .set("order_amount", temp.getOrder_amount()))
                .collect(Collectors.toList());

        data.put("rows", rows);
        return data;
    }

    @Override
    public List<ProductStats> getProductStatsGroupByCategory3(int date, int limit) {
        return productStatsMapper.getProductStatsGroupByCategory3(date, limit);
    }

    @Override
    public List<ProductStats> getProductStatsByTrademark(int date, int limit) {
        return productStatsMapper.getProductStatsByTrademark(date, limit);
    }

}
